//
//  addressView.m
//  何五路
//
//  Created by _ADY on 15/6/24.
//  Copyright (c) 2015年 _ADY. All rights reserved.
//

#import "addressView.h"

@implementation addressView
@synthesize alistArray;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        vFrame = frame;
        nameString = nameMString = nil;
        current = -1;
        page = 0;
        myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, frame.size.width, frame.size.height - 64)];
        myTableView.delegate = self;
        myTableView.dataSource = self;
        myTableView.backgroundColor = bgColor;
        [self addSubview:myTableView];
        
        UIView *v = [[UIView alloc] initWithFrame:CGRectZero];//清除多余分割线
        [myTableView setTableFooterView:v];
        
        [self performSelector:@selector(after) withObject:nil afterDelay:.2];
        
        
    }
    return self;
}

-(void)after
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        NSDictionary *ret = [SendService GetStatesByCountryId];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self gotoNs:ret];
            
        });
    });
    [self allButton];
    
}

-(void)after1
{
    NSMutableDictionary *postDic = [[NSMutableDictionary alloc] init];
    [postDic setObject:[[alistArray objectAtIndex:current] objectForKey:@"id"] forKey:@"provinceId"];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        NSDictionary *ret = [SendService GetCityByProvinceId:postDic];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self gotoNs:ret];

            
        });
    });
    
}

-(void)after2
{
    NSMutableDictionary *postDic = [[NSMutableDictionary alloc] init];
    [postDic setObject:[[alistArray objectAtIndex:current] objectForKey:@"id"] forKey:@"cityId"];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        NSDictionary *ret = [SendService GetDistrictByCityId:postDic];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self gotoNs:ret];
            
            
        });
    });
    
}

-(void)after3
{
    NSMutableDictionary *postDic = [[NSMutableDictionary alloc] init];
    [postDic setObject:[[alistArray objectAtIndex:current] objectForKey:@"id"] forKey:@"districtId"];
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        NSDictionary *ret = [SendService GetStreetByDistrictId:postDic];
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self gotoNs:ret];
            
            
        });
    });
    
}
-(void)gotoNs:(NSDictionary*)ret
{
//    NSLog(@"ret %@",ret);
    
//    [alistArray removeAllObjects];
    if (ret !=nil)
    {
        alistArray = [ret objectForKey:@"Entity"];
    }
    current = -1;

    [myTableView reloadData];
}
-(void)allButton
{
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 63.5, vFrame.size.width, .5)];
    lineView.backgroundColor = [UIColor colorWithRed:191/255.0 green:191/255.0 blue:191/255.0 alpha:1];
    [self addSubview:lineView];
    
    UIButton *inButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [inButton setFrame:CGRectMake(0,24, 50, 40)];
    [inButton setTitle:@"取消" forState:UIControlStateNormal];
    [inButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    inButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize+2];
    [inButton addTarget:self action:@selector(qxBction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:inButton];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(50,24, vFrame.size.width-100, 40)];
    titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize+2];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = 1;
    titleLabel.tag = 5;
    [self addSubview:titleLabel];
    
    UIButton *yesButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [yesButton setFrame:CGRectMake(vFrame.size.width-50,24, 50, 40)];
    [yesButton setTitle:@"确定" forState:UIControlStateNormal];
    [yesButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    yesButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:labelSize+2];
    [yesButton addTarget:self action:@selector(yesBction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:yesButton];
    
    
}

-(void)qxBction
{
    [self.delegate closeAddressView];
}

-(void)yesBction
{
    [SendService setaddress:nameString];
    [self.delegate closeAddressView];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return alistArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"screenTwoCell";
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        aTableView.showsVerticalScrollIndicator = NO;
        cell.tintColor = [UIColor colorWithRed:243/255.0 green:125/255.0 blue:125/255.0 alpha:1];//勾选颜色
    }
    int i = (int)[[cell.contentView subviews] count] - 1;
    for(;i >= 0 ; i--)
    {
        [[[cell.contentView subviews] objectAtIndex:i] removeFromSuperview];
    }
    aTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    cell.textLabel.text = [[alistArray objectAtIndex:indexPath.row] objectForKey:@"name"];
    cell.textLabel.textAlignment = 0;
    cell.textLabel.textColor = [UIColor blackColor];
    
    
    if(current== indexPath.row)
    {
        
        cell.accessoryType =  UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType =  UITableViewCellAccessoryNone;
    }
    // Configure the cell...
    return cell;
}




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
    current = indexPath.row;
    
    if (nameString.length == 0) {
        nameString = [[alistArray objectAtIndex:indexPath.row] objectForKey:@"name"];
    }
    else
        nameString = [NSString stringWithFormat:@"%@%@",nameString,[[alistArray objectAtIndex:indexPath.row] objectForKey:@"name"]];
    
    if (page == 2)
    {
        nameMString = nameString;
    }
    UILabel *titleLabel = (UILabel*)[self viewWithTag:5];
    if (alistArray.count >0)
        titleLabel.text = [[alistArray objectAtIndex:indexPath.row] objectForKey:@"name"];
    
    if (page == 3) {
        nameString = [NSString stringWithFormat:@"%@%@",nameMString,[[alistArray objectAtIndex:indexPath.row] objectForKey:@"name"]];
        [myTableView reloadData];
        return;
    }

    page ++;
    if (page == 1) {
        [self after1];
    }
    else if (page == 2) {
        [self after2];
    }
    else if (page == 3) {
        [self after3];
    }
    
}

@end
